/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbXES;
import storage.util.StorageManager;

import storage.dbobject.DbxesNameTable;

/**
 * 
 * @author fenyo rome
 *
 */
public class XESHelper {

	/**
	 * 将xes保存到LaUDMS数据库中
	 * 
	 * @param xes
	 *            要存储的模型对象
	 */
	public void saveXES(DbXES xes) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xes"));
		// sm.open();
		sm.setRowKey(xes.getRowKey());
		sm.put(DbXES.CL_NAME, xes.getName());
		sm.put(DbXES.CL_XES, xes.getXES());
		sm.put(DbXES.CL_CREATETIME, xes.getCreateTime());
		sm.put(DbXES.CL_BRANCH, xes.getBranch());
		System.out.println(xes.getBranch());
		sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xesNameTable"));
		sm.setRowKey(xes.getName());
		sm.put(DbxesNameTable.XESNAME, "1");
		sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xes"));
		sm.setRowKey(xes.getRowKey());
		if (xes.getClassify() != null) {
			if (xes.getClassify().length() > 0) {
				sm.put(DbXES.CL_CLASSIFY, xes.getClassify());
			}
		}
		System.out.println("save XES:" + xes.getName());
		// sm.close();
	}

	/**
	 * 将一系列xes保存到LaUDMS数据库中
	 * 
	 * @param xeses
	 *            要存储的模型对象列表
	 */
	public void saveXESs(List<DbXES> xeses) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xes"));
		// sm.open();
		for (int i = 0; i < xeses.size(); i++) {
			DbXES xes = xeses.get(i);
			sm.setRowKey(xes.getRowKey());
			sm.put(DbXES.CL_NAME, xes.getName());
			sm.put(DbXES.CL_XES, xes.getXES());
			sm.put(DbXES.CL_CREATETIME, xes.getCreateTime());
			sm.put(DbXES.CL_BRANCH, xes.getBranch());
			if (xes.getClassify() != null) {
				if (xes.getClassify().length() > 0) {
					sm.put(DbXES.CL_CLASSIFY, xes.getClassify());
				}
			}
		}
		// sm.close();
	}
/**
 * 
 * @param columnName
 * columnName
 * @param value
 * value
 * 
 * @return
 */
	public List<DbXES> getXESs(final String columnName, final String value) {
		List<DbXES> result = new LinkedList<DbXES>();
		// StorageManager sm = new StorageManager(StorageParameter.db_keySpace,
		// StorageParameter.db_columnFamily_xes);
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xes"));
		// sm.open();
		List<KeySlice> crs = sm.get(columnName, value);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String branch = null, name = null, xes = null, createTime = null, classify = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbXES.CL_BRANCH)) {
						branch = columnsValue;
					} else if (columnsName.equals(DbXES.CL_NAME)) {
						name = columnsValue;
					} else if (columnsName.equals(DbXES.CL_XES)) {
						xes = columnsValue;
					} else if (columnsName.equals(DbXES.CL_CREATETIME)) {
						createTime = columnsValue;
					} else if (columnsName.equals(DbXES.CL_CLASSIFY)) {
						classify = columnsValue;
					}
				}
				DbXES pXES = new DbXES(rowKey, branch, name, xes, createTime,
						classify);
				result.add(pXES);
			}
		} catch (Exception ex) {
			Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null,
					ex);
		} 
		return result;
	}
	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param value
	 * value
	 * @param columnName1
	 * columnName1
	 * 
	 * @param value1
	 * value1
	 * @return
	 */
	public List<DbXES> getXESsByNameBranch(final String columnName,
			final String value, final String columnName1, final String value1) {
		List<DbXES> result = new LinkedList<DbXES>();
		// StorageManager sm = new StorageManager(StorageParameter.db_keySpace,
		// StorageParameter.db_columnFamily_xes);
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xes"));
		// sm.open();
		List<KeySlice> crs = sm.getByTwoColumn(columnName, value, columnName1,
				value1);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String branch = null, name = null, xes = null, createTime = null, classify = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbXES.CL_BRANCH)) {
						branch = columnsValue;
					} else if (columnsName.equals(DbXES.CL_NAME)) {
						name = columnsValue;
					} else if (columnsName.equals(DbXES.CL_XES)) {
						xes = columnsValue;
					} else if (columnsName.equals(DbXES.CL_CREATETIME)) {
						createTime = columnsValue;
					} else if (columnsName.equals(DbXES.CL_CLASSIFY)) {
						classify = columnsValue;
					}
				}
				DbXES pXES = new DbXES(rowKey, branch, name, xes, createTime,
						classify);
				result.add(pXES);
			}
		} catch (Exception ex) {
			Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null,
					ex);
		} 
		return result;
	}
/**
 * 
 * @param columnName
 * columnName
 * @param value
 * value
 * @param columnName1
 * columnName1
 * @return
 */
	public List<String> getBranchListByName(final String columnName,
			final String value, final String columnName1) {
		List<String> result = new LinkedList();
		// StorageManager sm = new StorageManager(StorageParameter.db_keySpace,
		// StorageParameter.db_columnFamily_xes);
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xes"));
		// sm.open();
		List<KeySlice> crs = sm.getAllInOneColumnByAnother(DbXES.CL_NAME,
				value, DbXES.CL_BRANCH);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String branch = null, name = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbXES.CL_BRANCH)) {
						branch = columnsValue;
					} else if (columnsName.equals(DbXES.CL_NAME)) {
						name = columnsValue;
					}
				}
				// DbXES pXES = new DbXES(rowKey, branch, name, xes,
				// createTime);
				if (!result.contains(branch)) {
					result.add(branch);
				}
			}
			Collections.sort(crs); // sort the name of branch
		} catch (Exception ex) {
			Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null,
					ex);
		} 
		return result;
	}
	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param value
	 * value
	 * @param columnName1
	 * columnName1
	 * @return
	 */
	public List<String> getAllXESNameByClassify(final String columnName,
			final String value, final String columnName1) {
		List<String> results = new LinkedList();
		// StorageManager sm = new StorageManager(StorageParameter.db_keySpace,
		// StorageParameter.db_columnFamily_xes);
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xes"));
		// sm.open();
		List<KeySlice> crs = sm.getAllInOneColumnByAnother(columnName, value,
				columnName1);
		try {
			for (KeySlice ks : crs) {
				String rowKey = new String(ks.getKey());
				String result = null;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(columnName1)) {
						result = columnsValue;
					}
				}
				// DbXES pXES = new DbXES(rowKey, branch, name, xes,
				// createTime);
				if (!results.contains(result)) {
					results.add(result);
				}
			}
			Collections.sort(crs); // sort the name of branch
		} catch (Exception ex) {
			Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null,
					ex);
		} 
		return results;
	}
	/**
	 * 
	 * @return ArrayList<String>
	 */
	public ArrayList<String> getAllXESName() {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xesNameTable"));
		// sm.open();
		List<KeySlice> crs = sm.getAll();
		// List<KeySlice> crs = sm.getAll();
		ArrayList<String> xesNameList = new ArrayList<String>();
		try {
			for (KeySlice ks : crs) {
				String name = "";
				if (ks.getColumns().size() != 0) {
					name = new String(ks.getKey());

					xesNameList.add(name);
				}
			}
		} catch (Exception ex) {
			Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null,
					ex);
		} 
		return xesNameList;
	}
	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param name
	 * name
	 * @return
	 */
	public Hashtable<String, Integer> getSameNameNum(final String columnName,
			final String name) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xes"));
		Hashtable<String, Integer> branchInfo = new Hashtable<String, Integer>();
		int branchNum = 0;
		String branch = String.valueOf(branchNum);
		List<KeySlice> crs = sm.getByTwoColumn(columnName, name,
				DbXES.CL_BRANCH, branch);
		try {

			while (crs.size() > 0) {
				if (branchInfo.containsKey(branch)) {
					Integer newNum = new Integer(branchInfo.get(branch)
							.intValue() + 1);
					branchInfo.put(branch, newNum);
				} else {
					branchInfo.put(branch, new Integer(1));
				}
				branchNum++;
				branch = String.valueOf(branchNum);
				crs = sm.getByTwoColumn(columnName, name, DbXES.CL_BRANCH,
						branch);
			}
		} catch (Exception ex) {
			Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null,
					ex);
		}
		return branchInfo;
	}
	/**
	 * 
	 * @param columnName
	 * columnName
	 * @param name
	 * name
	 * @return
	 */
	public Hashtable<String, Integer> getSameNameNumOld(
			final String columnName, final String name) {
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xes"));
		List<KeySlice> crs = sm.get(columnName, name);
		Hashtable<String, Integer> branchInfo = new Hashtable<String, Integer>();
		try {
			for (KeySlice ks : crs) {
				boolean match = false;
				for (ColumnOrSuperColumn column : ks.getColumns()) {
					String columnsName = new String(column.getColumn()
							.getName());
					String columnsValue = new String(column.getColumn()
							.getValue());
					if (columnsName.equals(DbXES.CL_NAME)
							&& columnsValue.equalsIgnoreCase(name)) {
						match = true;

						break;
					}
				}
				if (match) {
					for (ColumnOrSuperColumn column : ks.getColumns()) {
						String columnsName = new String(column.getColumn()
								.getName());
						String columnsValue = new String(column.getColumn()
								.getValue());
						if (columnsName.equals(DbXES.CL_BRANCH)) {

							if (branchInfo.containsKey(columnsValue)) {
								Integer newNum = new Integer(branchInfo.get(
										columnsValue).intValue() + 1);
								branchInfo.put(columnsValue, newNum);
							} else {
								branchInfo.put(columnsValue, new Integer(1));
							}
						}
					
					}
				}


			}
		} catch (Exception ex) {
			Logger.getLogger(XESHelper.class.getName()).log(Level.SEVERE, null,
					ex);
		} 
		return branchInfo;
	}
	/**
	 * 
	 * @param rowKey
	 * rowKey
	 */
	public void deleteXES(final String rowKey) {
		// StorageManager sm = new StorageManager(StorageParameter.db_keySpace,
		// StorageParameter.db_columnFamily_xes);
		PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
		StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"),
				x1.getProperties("db_columnFamily_xes"));
		// sm.open();
		sm.delete(rowKey);
		// sm.close();
	}
	public static void main(String [] args ) {
		XESHelper xh = new XESHelper();
		List<String> names = xh.getAllXESName();
		for(String name : names) {
			System.out.println(name);
		}
		
	}
}
